package implementation {
import com.greensock.TweenLite;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;

internal class TestItem extends Sprite {

	private const BORDER : uint = 20;
	private const WIDTH : uint = 60;
	private const HEIGHT : uint = 60;
	private const X : int = -(WIDTH / 2);
	private const Y : int = -30;

	private var top_bar : Sprite;
	private var bottom_bar : Sprite;
	private var back_bar : Sprite;
	private var stay_open : Boolean = false;

	public function TestItem(label : String) {
		makeTopBar();
		makeBottomBar();
		makeBackBar();

		addLabels(label);
		addMouseHandlers();
		contract();
	}

	private function makeTopBar() : void {
		top_bar = new Sprite();
		top_bar.graphics.beginFill(0x666666, 0.8);
		top_bar.graphics.drawRoundRect(X, Y, WIDTH, 2 * BORDER, BORDER);
		top_bar.graphics.drawRoundRect(X, Y + BORDER, WIDTH, BORDER, BORDER);
		top_bar.graphics.endFill();
		addChild(top_bar);
		top_bar.mouseEnabled = false;
	}

	private function makeBottomBar() : void {
		bottom_bar = new Sprite();
		bottom_bar.graphics.beginFill(0x333333, 0.8);
		bottom_bar.graphics.drawRoundRect(X, Y, WIDTH, BORDER, BORDER);
		bottom_bar.graphics.endFill();
		addChild(bottom_bar);
		bottom_bar.mouseEnabled = false;
	}

	private function makeBackBar() : void {
		back_bar = new Sprite();
		back_bar.graphics.beginFill(0xffffff * Math.random(), 0.6);
		back_bar.graphics.drawRoundRect(X, Y, WIDTH, HEIGHT, BORDER);
		back_bar.graphics.endFill();
		addChildAt(back_bar, 0);
		back_bar.mouseEnabled = false;
	}

	private function addLabels(label : String) : void {
		var text_field : TextField = makeNewTextField(label);
		addTextFieldToBottomBar(text_field);
		addImageFieldToTopBar(text_field);
	}

	private function makeNewTextField(label : String) : TextField {
		var format : TextFormat = new TextFormat("Tahoma, _sans", 12, 0xeeeeee);
		var text_field : TextField = new TextField();
		text_field.defaultTextFormat = format;
		text_field.multiline = false;
		text_field.selectable = false;
		text_field.mouseEnabled = false;
		text_field.autoSize = TextFieldAutoSize.LEFT;
		text_field.text = label;
		return text_field;
	}

	private function addTextFieldToBottomBar(textField : TextField) : void {
		textField.x = -textField.width / 2;
		textField.y = -30;
		bottom_bar.addChild(textField);
	}

	private function addImageFieldToTopBar(textField : TextField) : void {
		var bitmap_data : BitmapData = new BitmapData(textField.width, textField.height, true, 0);
		bitmap_data.draw(textField);

		var bitmap : Bitmap = new Bitmap(bitmap_data);
		bitmap.x = (- bitmap.width) >> 1;
		bitmap.y = -30;
		top_bar.addChild(bitmap);
	}

	private function addMouseHandlers() : void {
		addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
		addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
	}

	private function onMouseOver(event : MouseEvent) : void {
		expand();
	}

	private function onMouseOut(event : MouseEvent) : void {
		if (!stay_open) {
			contract();
		}
	}

	private function onMouseDown(event : MouseEvent) : void {
		stay_open = !stay_open
	}

	private function expand() : void {
		TweenLite.to(top_bar, 0.5, { y: 0 });
		TweenLite.to(bottom_bar, 0.5, { y: 40 });
		TweenLite.to(back_bar, 0.5, { height: 60, alpha: 0.8 });
	}

	private function contract() : void {
		TweenLite.to(top_bar, 0.5, { y: 10 });
		TweenLite.to(bottom_bar, 0.5, { y: 30});
		TweenLite.to(back_bar, 0.5, { height: 40, alpha: 0})
	}

}
}
